package com.kou.backend.dto.user;

import com.kou.backend.entity.UserInfoDO;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Security需要的用户对象
 *
 * @author: Chaojie.Kou
 * @since: 2023/8/31 17:14
 */
@Data
public class UserDetailSecurityDTO implements UserDetails {
    private UserInfoDO userInfoDO;

    private List<String> permValues;

    public UserDetailSecurityDTO(UserInfoDO userInfoDO, List<String> permValues) {
        this.userInfoDO = userInfoDO;
        this.permValues = permValues;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //KouTODO:2023/8/31 20:33 kou 角色权限管理完善后需修改
        return permValues.stream()
                .map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    /**
     * 不使用Security的密码验证
     *
     * @return **
     */
    @Override
    public String getPassword() {
        return "**";
    }

    /**
     * 重写getUsername 使其返回用户帐号
     *
     * @return 用户帐号
     */
    @Override
    public String getUsername() {
        return userInfoDO.getUserAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
